<?php
class Zend_View_Helper_ModernSelect extends Zend_View_Helper_FormElement
{
    public function modernSelect($name = null, $value = NULL, $attribs = NULL, $options = Null, $listsep = Null)
    {
        $idStr = ($name != null)?('id="' . $name .'"'):('');
        $itemClass = 'modern_item';
        $attrString = '';
        $styleArray = array(
          'border' => '1px solid #CCCCCC', 
          'border-radius' => '5px 5px 5px 5px',
          'cursor' => 'pointer',
          'width' => '120px',
          'background-color' => '#FFF',
          'padding' => '2px',
          'padding-left' => '4px'
        );
        $styleStr = '';
        if (isset($attribs['style']) && is_array($attribs['style'])){
            $styleArray = array_merge($styleArray, $attribs['style']);
        }
        foreach ($styleArray as $property => $val)
        {
            $styleStr .= $property . ': ' . $val . '; ';
        }
        if (isset($attribs['style']) && is_string($attribs['style'])){
            $styleStr .= ' ' . $attribs['style'];
        }
        $additionalClass = isset($attribs['additional_class'])?($attribs['additional_class'] . ' '):('');
        if ($attribs != null && isset($attribs['parameters']) && is_array($attribs['parameters'])){
            foreach ($attribs['parameters'] as $key => $value){
                $attrString .= $key . '="' . $value . '" ';
            }
        }
        $html = "<div " . $idStr . " " . $attrString . " style='" . $styleStr . "'>";
        $htmlOptionsArray = array();
        $keys = array_keys($options);
        if ($value != null and in_array($value, $keys)){
            $html .= "<div class='selected_item' style='min-height: 16px' rel='" . $value . "'><span class='" . $additionalClass . $value . "'></span>" . $options[$value] . "<span class='drop down'></span></div>";
            $selected = $value;
        }else{
            $html .= "<div class='selected_item' style='min-height: 16px' rel='" . $keys[0] . "'><span class='" . $additionalClass . $keys[0] . "'></span>" . $options[$keys[0]] . "<span class='drop down'></span></div>";
            $selected = $keys[0];
        }
        $html .= "<div class='sub' style='min-width: " . $styleArray['width'] . "; background-color: " . $styleArray['background-color']  . "; position: absolute; display: none; padding-top: 4px; border: 1px solid rgb(204, 204, 204); border-top: 0; margin-top: 2px;'>";
        
        foreach ($options as $key => $option){
           $html .= '<div class="' . $itemClass . '" rel="' . $key . '" style="padding-left: ' . $styleArray['padding-left'] . '"><span class="' . $additionalClass . $key . '"></span>' . $option . '</div>';
        }
        $html .= '</div>';
        $html .= '<input type="hidden" name="' . $name . '" value="' . $selected . '"/>';
        $html .= '</div>';
        
        $html .= '<script type="text/javascript">
                    $("#' . $name . '").click(function(){
                        if ($("#' . $name . ' .sub").css("display") == "none"){
                            $("#' . $name . ' .sub").slideDown("fast");
                        }else{
                           $("#' . $name . ' .sub").slideUp("fast");
                        }
                    });
                    $("#' . $name . ' .' . $itemClass . '").click(function(){
                        var val = $(this).attr("rel");
                        var selected = $("#' . $name . ' .selected_item").attr("rel");
                        if (selected != val){
                            var html = this.innerHTML + \'<span class="drop down"></span>\';
                            $("#' . $name . ' .selected_item").html(html);
                            $("#' . $name . ' .selected_item").attr("rel", val);
                            $("#' . $name . ' input").val(val);
                        }
                        $("#' . $name . ' .sub").slideUp("fast");
                    });
                    $("#' . $name . '").mouseleave(function(){
                        $("#' . $name . ' .sub").slideUp("fast");
                    });
                </script>';
        
        return $html;
    }
}
?>